```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyBatis Executor与Statement管理机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .dropcap:first-letter {
            float: left;
            font-size: 4.5em;
            line-height: 0.8;
            margin-right: 0.1em;
            color: #6e8efb;
            font-weight: 700;
        }
        .code-block {
            background-color: #2d3748;
            color: #f8fafc;
            border-radius: 0.5rem;
            overflow-x: auto;
        }
        .code-keyword {
            color: #f472b6;
        }
        .code-method {
            color: #60a5fa;
        }
        .code-string {
            color: #34d399;
        }
        .code-comment {
            color: #9ca3af;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl flex flex-col items-center text-center">
            <div class="w-20 h-20 bg-white rounded-full flex items-center justify-center mb-6 shadow-lg">
                <i class="fas fa-database text-3xl text-purple-600"></i>
            </div>
            <h1 class="text-4xl md:text-5xl font-bold mb-4 tracking-tight">MyBatis 核心机制解析</h1>
            <h2 class="text-2xl md:text-3xl font-medium mb-6 opacity-90">Executor与Statement的管理艺术</h2>
            <p class="text-xl max-w-2xl leading-relaxed opacity-90">
                MyBatis框架中，Executor和Statement的管理是数据库操作的核心机制。深入理解这一架构，将帮助您编写更高效、更可靠的数据库访问代码。
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-16">
        <!-- Intro Section -->
        <section class="mb-20">
            <div class="dropcap text-gray-700 text-lg leading-relaxed">
                在MyBatis框架中，`Executor`和`Statement`的管理是数据库操作的核心。`Executor`负责执行SQL语句的调度，而`Statement`则是执行SQL语句的具体工具。本文将深入剖析这两大核心组件的职责、协作关系以及性能优化策略，帮助开发者全面掌握MyBatis的执行机制。
            </div>
        </section>

        <!-- Executor Overview Card -->
        <section class="mb-20">
            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center mr-4">
                            <i class="fas fa-cogs text-blue-600 text-xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">1. Executor的职责与功能</h2>
                    </div>
                    <div class="space-y-4 text-gray-700">
                        <p><code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">Executor</code>是MyBatis中的核心组件之一，它负责根据传入的SQL语句执行数据库操作。<code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">Executor</code>的实现类有多种，最常用的是<code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">SimpleExecutor</code>、<code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">ReuseExecutor</code>和<code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">BatchExecutor</code>。它们的共同职责是管理数据库连接、执行SQL以及处理相关的事务管理等。</p>
                        
                        <div class="border-l-4 border-blue-500 pl-4 my-4">
                            <p class="font-medium text-gray-800"><code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">Executor</code>的职责包括：</p>
                            <ul class="list-disc pl-6 space-y-2 mt-2">
                                <li>执行SQL语句，获取结果集</li>
                                <li>处理SQL语句的缓存</li>
                                <li>管理事务的提交与回滚</li>
                                <li>支持不同的执行策略（简单执行、复用执行、批量执行等）</li>
                            </ul>
                        </div>
                        
                        <p>通过使用不同类型的<code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">Executor</code>，MyBatis能够根据需求选择不同的执行方式来优化性能。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Execution Flow Section -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <span class="w-8 h-8 bg-blue-600 rounded-full text-white flex items-center justify-center mr-4">2</span>
                Executor的执行流程
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <p class="text-gray-700 mb-6"><code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">Executor</code>的执行流程从用户调用<code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">SqlSession</code>的相关方法开始，最终通过<code class="bg-blue-50 text-blue-600 px-2 py-1 rounded">Executor</code>执行SQL语句。以下是一个典型的执行流程：</p>
                    
                    <div class="bg-white rounded-lg p-6 shadow-sm border border-gray-100 mb-6">
                        <ol class="space-y-4 list-decimal pl-5 text-gray-700">
                            <li><span class="font-medium">创建SqlSession</span>：当应用程序调用<code class="bg-gray-100 px-1 rounded">SqlSessionFactory.openSession()</code>时，会创建一个<code class="bg-gray-100 px-1 rounded">SqlSession</code>对象，<code class="bg-gray-100 px-1 rounded">SqlSession</code>的创建会初始化<code class="bg-gray-100 px-1 rounded">Executor</code></li>
                            <li><span class="font-medium">选择Executor类型</span>：<code class="bg-gray-100 px-1 rounded">SqlSession</code>根据配置选择使用哪种类型的<code class="bg-gray-100 px-1 rounded">Executor</code>（如<code class="bg-gray-100 px-1 rounded">SimpleExecutor</code>、<code class="bg-gray-100 px-1 rounded">ReuseExecutor</code>等）</li>
                            <li><span class="font-medium">执行SQL</span>：在调用<code class="bg-gray-100 px-1 rounded">selectList()</code>、<code class="bg-gray-100 px-1 rounded">insert()</code>、<code class="bg-gray-100 px-1 rounded">update()</code>等方法时，<code class="bg-gray-100 px-1 rounded">Executor</code>会委托给底层的<code class="bg-gray-100 px-1 rounded">StatementHandler</code>来创建<code class="bg-gray-100 px-1 rounded">Statement</code>对象，并执行SQL语句</li>
                            <li><span class="font-medium">获取结果集</span>：执行SQL语句后，<code class="bg-gray-100 px-1 rounded">Executor</code>会获取结果集并封装成相应的对象返回给调用方</li>
                        </ol>
                    </div>
                </div>
                
                <div class="code-block p-6 rounded-lg">
                    <pre><code class="language-java"><span class="code-keyword">public</span> List&lt;T&gt; selectList(String statement, Object parameter) {
    Executor executor = sqlSession.getExecutor();
    <span class="code-keyword">return</span> executor.query(statement, parameter, RowBounds.DEFAULT, null);
}</code></pre>
                    <div class="mt-4 text-gray-400 text-sm">
                        <p class="flex items-center mb-2"><span class="w-3 h-3 bg-pink-500 rounded-full mr-2"></span> <code class="bg-gray-800 text-pink-400 px-1">sqlSession.getExecutor()</code>：获取当前<code class="bg-gray-800 text-pink-400 px-1">SqlSession</code>所使用的<code class="bg-gray-800 text-pink-400 px-1">Executor</code></p>
                        <p class="flex items-center"><span class="w-3 h-3 bg-blue-500 rounded-full mr-2"></span> <code class="bg-gray-800 text-blue-400 px-1">executor.query()</code>：调用<code class="bg-gray-800 text-blue-400 px-1">Executor</code>的<code class="bg-gray-800 text-blue-400 px-1">query</code>方法来执行SQL查询</p>
                    </div>
                    <p class="mt-4 text-gray-400 text-sm flex items-start">
                        <i class="fas fa-info-circle text-blue-400 mt-1 mr-2"></i>
                        在这里，<code class="bg-gray-800 text-gray-300 px-1">Executor</code>会根据<code class="bg-gray-800 text-gray-300 px-1">statement</code>的配置以及<code class="bg-gray-800 text-gray-300 px-1">parameter</code>参数，决定如何执行查询。
                    </p>
                </div>
            </div>
        </section>

        <!-- StatementHandler Section -->
        <section class="mb-20">
            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center mr-4">
                            <i class="fas fa-link text-purple-600 text-xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">3. StatementHandler与Statement的关系</h2>
                    </div>
                    
                    <div class="space-y-4 text-gray-700">
                        <p>在MyBatis中，<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">StatementHandler</code>负责创建和处理<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">Statement</code>对象，<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">Statement</code>是用于执行SQL的工具。<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">Executor</code>通过<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">StatementHandler</code>来管理和执行SQL语句。</p>
                        
                        <div class="border-l-4 border-purple-500 pl-4 my-4">
                            <p class="font-medium text-gray-800">具体来说，<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">StatementHandler</code>的职责是：</p>
                            <ul class="list-disc pl-6 space-y-2 mt-2">
                                <li>根据SQL语句和参数生成对应的<code class="bg-purple-50 text-purple-600 px-1 rounded">Statement</code>对象</li>
                                <li>设置SQL语句的参数</li>
                                <li>执行SQL语句并返回结果</li>
                            </ul>
                        </div>
                        
                        <p><code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">Statement</code>则是由<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">StatementHandler</code>创建的，它是执行SQL的核心工具。在不同的<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">Executor</code>类型中，<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">StatementHandler</code>的实现有所不同。</p>
                        
                        <div class="code-block p-6 rounded-lg mt-6">
                            <pre><code class="language-java"><span class="code-keyword">public interface</span> StatementHandler {
    <span class="code-keyword">Statement</span> prepare(<span class="code-keyword">Connection</span> connection, <span class="code-keyword">Integer</span> transactionTimeout) <span class="code-keyword">throws</span> <span class="code-keyword">SQLException</span>;
    <span class="code-keyword">void</span> parameterize(<span class="code-keyword">Statement</span> statement) <span class="code-keyword">throws</span> <span class="code-keyword">SQLException</span>;
    &lt;T&gt; List&lt;T&gt; query(<span class="code-keyword">Statement</span> statement, <span class="code-keyword">ResultHandler</span> resultHandler) <span class="code-keyword">throws</span> <span class="code-keyword">SQLException</span>;
}</code></pre>
                            <div class="mt-4 text-gray-400 text-sm space-y-2">
                                <p class="flex items-center"><span class="w-3 h-3 bg-blue-500 rounded-full mr-2"></span> <code class="bg-gray-800 text-blue-400 px-1">prepare()</code>：根据数据库连接准备<code class="bg-gray-800 text-blue-400 px-1">Statement</code>对象</p>
                                <p class="flex items-center"><span class="w-3 h-3 bg-green-500 rounded-full mr-2"></span> <code class="bg-gray-800 text-green-400 px-1">parameterize()</code>：为<code class="bg-gray-800 text-green-400 px-1">Statement</code>设置SQL参数</p>
                                <p class="flex items-center"><span class="w-3 h-3 bg-purple-500 rounded-full mr-2"></span> <code class="bg-gray-800 text-purple-400 px-1">query()</code>：执行查询操作，并返回查询结果</p>
                            </div>
                        </div>
                        
                        <p class="mt-4 text-gray-700">通过这些方法，<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">Executor</code>可以创建、配置和执行<code class="bg-purple-50 text-purple-600 px-2 py-1 rounded">Statement</code>对象，从而实现SQL语句的执行。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Transaction Management Section -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <span class="w-8 h-8 bg-green-600 rounded-full text-white flex items-center justify-center mr-4">4</span>
                事务管理与Executor
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <p class="text-gray-700 mb-6"><code class="bg-green-50 text-green-600 px-2 py-1 rounded">Executor</code>不仅负责SQL语句的执行，还与事务管理紧密关联。在MyBatis中，事务管理通常通过<code class="bg-green-50 text-green-600 px-2 py-1 rounded">SqlSession</code>来进行，<code class="bg-green-50 text-green-600 px-2 py-1 rounded">SqlSession</code>会根据配置选择合适的事务管理策略（如JDBC事务、Spring事务等）。</p>
                    
                    <div class="bg-white rounded-lg p-6 shadow-sm border border-gray-100 mb-6">
                        <p class="font-medium text-gray-800 mb-3"><code class="bg-green-50 text-green-600 px-2 py-1 rounded">Executor</code>的事务管理功能主要体现在以下两个方面：</p>
                        
                        <div class="space-y-4">
                            <div class="flex items-start">
                                <div class="flex-shrink-0 h-8 w-8 rounded-full bg-green-100 flex items-center justify-center mt-1 mr-3">
                                    <i class="fas fa-check-circle text-green-600"></i>
                                </div>
                                <div>
                                    <p class="font-medium text-gray-800">事务的提交与回滚</p>
                                    <p class="text-gray-700 text-sm"><code class="bg-gray-100 px-1 rounded">Executor</code>在执行SQL语句时会检查事务状态，如果操作成功，提交事务；如果出现异常，则回滚事务</p>
                                </div>
                            </div>
                            
                            <div class="flex items-start">
                                <div class="flex-shrink-0 h-8 w-8 rounded-full bg-green-100 flex items-center justify-center mt-1 mr-3">
                                    <i class="fas fa-layer-group text-green-600"></i>
                                </div>
                                <div>
                                    <p class="font-medium text-gray-800">事务的嵌套执行</p>
                                    <p class="text-gray-700 text-sm">当多个SQL操作需要在同一事务中执行时，<code class="bg-gray-100 px-1 rounded">Executor</code>会确保这些操作在一个事务中执行，避免数据不一致</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="code-block p-6 rounded-lg">
                    <pre><code class="language-java"><span class="code-keyword">public void</span> commit(<span class="code-keyword">boolean</span> force) {
    <span class="code-keyword">if</span> (force || !<span class="code-keyword">this</span>.autoCommit) {
        executor.commit();
    }
}</code></pre>
                    <div class="mt-4 text-gray-400 text-sm">
                        <p class="flex items-center"><span class="w-3 h-3 bg-green-500 rounded-full mr-2"></span> <code class="bg-gray-800 text-green-400 px-1">executor.commit()</code>：如果当前事务的操作成功，调用<code class="bg-gray-800 text-green-400 px-1">Executor</code>的<code class="bg-gray-800 text-green-400 px-1">commit()</code>方法提交事务</p>
                    </div>
                    <p class="mt-4 text-gray-400 text-sm flex items-start">
                        <i class="fas fa-info-circle text-green-400 mt-1 mr-2"></i>
                        事务的管理保证了SQL执行的原子性、隔离性和一致性。
                    </p>
                </div>
            </div>
        </section>

        <!-- Statement Creation and Caching Section -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <span class="w-8 h-8 bg-yellow-600 rounded-full text-white flex items-center justify-center mr-4">5</span>
                Statement的创建与缓存
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                <div class="p-8">
                    <p class="text-gray-700 mb-6"><code class="bg-yellow-50 text-yellow-600 px-2 py-1 rounded">Statement</code>的创建是SQL执行过程中的重要环节。MyBatis通过<code class="bg-yellow-50 text-yellow-600 px-2 py-1 rounded">StatementHandler</code>来创建<code class="bg-yellow-50 text-yellow-600 px-2 py-1 rounded">Statement</code>对象，并通过不同的执行策略来优化性能。</p>
                    
                    <p class="text-gray-700 mb-6">MyBatis支持<code class="bg-yellow-50 text-yellow-600 px-2 py-1 rounded">Statement</code>的缓存，缓存可以有效地减少重复的SQL查询，从而提高性能。<code class="bg-yellow-50 text-yellow-600 px-2 py-1 rounded">Executor</code>会根据SQL语句的不同，判断是否使用缓存。</p>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="bg-blue-50 rounded-lg p-6">
                            <div class="flex items-center mb-3">
                                <div class="w-8 h-8 bg-blue-200 rounded-full flex items-center justify-center mr-3">
                                    <span class="text-blue-700 font-bold">1</span>
                                </div>
                                <h3 class="font-bold text-blue-800">一级缓存</h3>
                            </div>
                            <p class="text-gray-700 text-sm"><code class="bg-blue-100 text-blue-600 px-1 rounded">Executor</code>内部会缓存<code class="bg-blue-100 text-blue-600 px-1 rounded">Statement</code>对象及其执行结果。一级缓存的作用范围仅限于<code class="bg-blue-100 text-blue-600 px-1 rounded">SqlSession</code>级别，在同一个<code class="bg-blue-100 text-blue-600 px-1 rounded">SqlSession</code>内重复执行相同的查询时，会直接返回缓存的结果，而不必重新执行SQL</p>
                        </div>
                        
                        <div class="bg-purple-50 rounded-lg p-6">
                            <div class="flex items-center mb-3">
                                <div class="w-8 h-8 bg-purple-200 rounded-full flex items-center justify-center mr-3">
                                    <span class="text-purple-700 font-bold">2</span>
                                </div>
                                <h3 class="font-bold text-purple-800">二级缓存</h3>
                            </div>
                            <p class="text-gray-700 text-sm"><code class="bg-purple-100 text-purple-600 px-1 rounded">Executor</code>也支持跨<code class="bg-purple-100 text-purple-600 px-1 rounded">SqlSession</code>的缓存，通过<code class="bg-purple-100 text-purple-600 px-1 rounded">Configuration</code>的配置来启用</p>
                        </div>
                    </div>
                    
                    <div class="code-block p-6 rounded-lg mt-6">
                        <pre><code class="language-java"><span class="code-keyword">public class</span> CacheExecutor <span class="code-keyword">implements</span> Executor {
    <span class="code-keyword">private final</span> Executor delegate;
    <span class="code-keyword">private final</span> Cache cache;

    <span class="code-keyword">public</span> CacheExecutor(Executor delegate, Cache cache) {
        <span class="code-keyword">this</span>.delegate = delegate;
        <span class="code-keyword">this</span>.cache = cache;
    }

    @Override
    <span class="code-keyword">public</span> &lt;E&gt; List&lt;E&gt; query(String statement, Object parameter) {
        <span class="code-comment">// 查询缓存</span>
        List&lt;E&gt; result = cache.get(statement, parameter);
        <span class="code-keyword">if</span> (result == <span class="code-keyword">null</span>) {
            result = delegate.query(statement, parameter); <span class="code-comment">// 如果缓存没有，执行SQL</span>
            cache.put(statement, parameter, result); <span class="code-comment">// 将结果缓存</span>
        }
        <span class="code-keyword">return</span> result;
    }
}</code></pre>
                        <div class="mt-4 text-gray-400 text-sm">
                            <p class="flex items-center"><span class="w-3 h-3 bg-yellow-500 rounded-full mr-2"></span> <span class="font-medium">缓存逻辑</span>：首先尝试从缓存中获取数据，如果没有则执行SQL查询，并将结果存入缓存</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- ReuseExecutor Section -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <span class="w-8 h-8 bg-indigo-600 rounded-full text-white flex items-center justify-center mr-4">6</span>
                ReuseExecutor与Statement复用
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8 items-center">
                <div>
                    <p class="text-gray-700 mb-6"><code class="bg-indigo-50 text-indigo-600 px-2 py-1 rounded">ReuseExecutor</code>是MyBatis中一种常见的<code class="bg-indigo-50 text-indigo-600 px-2 py-1 rounded">Executor</code>实现，它的特点是复用<code class="bg-indigo-50 text-indigo-600 px-2 py-1 rounded">Statement</code>。在默认的<code class="bg-indigo-50 text-indigo-600 px-2 py-1 rounded">SimpleExecutor</code>中，每次执行SQL时，都会创建新的<code class="bg-indigo-50 text-indigo-600 px-2 py-1 rounded">Statement</code>对象，而<code class="bg-indigo-50 text-indigo-600 px-2 py-1 rounded">ReuseExecutor</code>会在同一个<code class="bg-indigo-50 text-indigo-600 px-2 py-1 rounded">SqlSession</code>中复用已经创建的<code class="bg-indigo-50 text-indigo-600 px-2 py-1 rounded">Statement</code>，从而提高性能。</p>
                    
                    <div class="bg-indigo-50 border-l-4 border-indigo-500 pl-4 py-2 my-4">
                        <p class="font-medium text-indigo-800">复用缓存：</p>
                        <p class="text-gray-700 text-sm">通过缓存<code class="bg-indigo-100 text-indigo-600 px-1 rounded">Statement</code>对象，<code class="bg-indigo-100 text-indigo-600 px-1 rounded">ReuseExecutor</code>避免了频繁创建新的<code class="bg-indigo-100 text-indigo-600 px-1 rounded">Statement</code>，提高了性能</p>
                    </div>
                </div>
                
                <div class="code-block p-6 rounded-lg">
                    <pre><code class="language-java"><span class="code-keyword">public class</span> ReuseExecutor <span class="code-keyword">implements</span> Executor {
    <span class="code-keyword">private final</span> Map&lt;String, Statement&gt; statementCache = <span class="code-keyword">new</span> HashMap&lt;&gt;();

    @Override
    <span class="code-keyword">public</span> &lt;E&gt; List&lt;E&gt; query(String statement, Object parameter) {
        <span class="code-keyword">Statement</span> stmt = statementCache.get(statement); <span class="code-comment">// 获取缓存的Statement</span>
        <span class="code-keyword">if</span> (stmt == <span class="code-keyword">null</span>) {
            stmt = createStatement(statement); <span class="code-comment">// 如果没有缓存，则创建新的Statement</span>
            statementCache.put(statement, stmt); <span class="code-comment">// 缓存Statement</span>
        }
        <span class="code-keyword">return</span> executeStatement(stmt); <span class="code-comment">// 执行Statement</span>
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- BatchExecutor Section -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 flex items-center">
                <span class="w-8 h-8 bg-red-600 rounded-full text-white flex items-center justify-center mr-4">7</span>
                BatchExecutor与批量执行
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8 items-center">
                <div class="order-last md:order-first">
                    <p class="text-gray-700 mb-6"><code class="bg-red-50 text-red-600 px-2 py-1 rounded">BatchExecutor</code>是一种优化<code class="bg-red-50 text-red-600 px-2 py-1 rounded">Executor</code>，它用于处理批量的SQL执行操作。当需要执行多个相同类型的SQL操作时，<code class="bg-red-50 text-red-600 px-2 py-1 rounded">BatchExecutor</code>会将多个SQL语句组合在一起一次性提交到数据库，从而减少数据库交互的次数。</p>
                    
                    <div class="bg-red-50 border-l-4 border-red-500 pl-4 py-2 my-4">
                        <p class="font-medium text-red-800">批量操作：</p>
                        <p class="text-gray-700 text-sm">将多个SQL语句合并成一个批量操作，减少数据库连接和交互的次数，提高性能</p>
                    </div>
                </div>
                
                <div class="code-block p-6 rounded-lg order-first md:order-last">
                    <pre><code class="language-java"><span class="code-keyword">public class</span> BatchExecutor <span class="code-keyword">implements</span> Executor {
    <span class="code-keyword">private final</span> List&lt;Statement&gt; statements = <span class="code-keyword">new</span> ArrayList&lt;&gt;();

    @Override
    <span class="code-keyword">public</span> &lt;E&gt; List&lt;E&gt; query(String statement, Object parameter) {
        statements.add(createBatchStatement(statement)); <span class="code-comment">// 将SQL语句加入批量操作</span>
        <span class="code-keyword">return</span> <span class="code-keyword">new</span> ArrayList&lt;&gt;();
    }

    <span class="code-keyword">public void</span> executeBatch() {
        <span class="code-comment">// 执行所有批量操作</span>
        <span class="code-keyword">for</span> (<span class="code-keyword">Statement</span> stmt : statements) {
            executeStatement(stmt);
        }
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Summary Diagram -->
        <section class="mb-20">
            <div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <div class="w-12 h-12 bg-teal-100 rounded-lg flex items-center justify-center mr-4">
                            <i class="fas fa-project-diagram text-teal-600 text-xl"></i>
                        </div>
                        <h2 class="text-2xl font-bold text-gray-800">MyBatis执行机制关系图</h2>
                    </div>
                    
                    <div class="mermaid">
                        graph TD
                            A[SqlSession] -->|委托执行| B[Executor]
                            B -->|使用| C[StatementHandler]
                            C -->|创建| D[Statement]
                            B -->|管理| E[Transaction]
                            B -->|使用| F[Cache]
                            
                            B --> G[SimpleExecutor]
                            B --> H[ReuseExecutor]
                            B --> I[BatchExecutor]
                            G -->|每次创建新Statement| D
                            H -->|复用Statement| D
                            I -->|批量执行Statement| D
                            
                            style A fill:#f0f9ff,stroke:#38bdf8
                            style B fill:#f0fdf4,stroke:#4ade80
                            style C fill:#f8fafc,stroke:#94a3b8
                            style D fill:#fff1f2,stroke:#fb7185
                            style E fill:#fef2f2,stroke:#f87171
                            style F fill:#ecfdf5,stroke:#10b981
                            style G fill:#f0f9ff,stroke:#7dd3fc
                            style H fill:#f0f9ff,stroke:#7dd3fc
                            style I fill:#f0f9ff,stroke:#7dd3fc
                    </div>
                    
                    <div class="mt-6 text-gray-700">
                        <p>上图展示了MyBatis核心组件之间的协作关系：</p>
                        <ul class="list-disc pl-6 mt-2 space-y-1">
                            <li><code class="bg-gray-100 px-1 rounded">SqlSession</code>作为用户API入口，将操作委托给<code class="bg-gray-100 px-1 rounded">Executor</code></li>
                            <li><code class="bg-gray-100 px-1 rounded">Executor</code>负责SQL执行的整体调度，管理事务和缓存</li>
                            <li><code class="bg-gray-100 px-1 rounded">StatementHandler</code>负责创建和配置<code class="bg-gray-100 px-1 rounded">Statement</code>对象</li>
                            <li>不同类型的<code class="bg-gray-100 px-1 rounded">Executor</code>（Simple/Reuse/Batch）实现了不同的执行策略</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center">
                <div class="text-2xl font-bold text-white mb-2">技术小馆</div>
                <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors duration-200">
                    <i class="fas fa-external-link-alt mr-1"></i>
                    http://www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```